<template>
  <li v-if="comment && comment.user" class="comment">
    <div class="by">
      <router-link :to="'/user/' + comment.user">
        {{ comment.user }}
      </router-link>
      {{ comment.time_ago }}
    </div>
    <div class="text" v-html="comment.content" />
    <div
      v-if="comment.comments && comment.comments.length"
      :class="{ open }"
      class="toggle"
    >
      <a @click="open = !open">
        {{
          open
            ? "[-]"
            : "[+] " + pluralize(comment.comments.length) + "&nbsp;collapsed"
        }}</a
      >
    </div>
    <ul v-show="open" class="comment-children">
      <comment
        v-for="childComment in comment.comments"
        :key="childComment.id"
        :comment="childComment"
      />
    </ul>
  </li>
</template>

<script>
import { ref, defineComponent } from "vue";

export default defineComponent({
  props: {
    comment: {
      type: Object,
      required: true,
    },
  },
  setup() {
    const open = ref(true);
    const pluralize = (n) => n + (n === 1 ? " reply" : " replies");
    return { open, pluralize };
  },
});
</script>

<style lang="scss">
.comment-children {
  .comment-children {
    margin-left: 1.5em;
  }
}

.comment {
  border-top: 1px solid #eee;
  position: relative;

  .by,
  .text,
  .toggle {
    font-size: 0.9em;
    margin: 1em 0;
  }

  .by {
    color: #222;

    a {
      color: #222;
      text-decoration: underline;
    }
  }

  .text {
    overflow-wrap: break-word;

    a:hover {
      color: #111;
    }

    pre {
      white-space: pre-wrap;
    }
  }

  .toggle {
    background-color: #fffbf2;
    padding: 0.3em 0.5em;
    border-radius: 4px;

    a {
      color: #222;
      cursor: pointer;
    }

    &.open {
      padding: 0;
      background-color: transparent;
      margin-bottom: -0.5em;
    }
  }
}
</style>
